#!/bin/sh
set -eu

version=2.3.7

case $(uname -m) in
  x86_64) tag=x86_64_linux;;
  aarch64) tag=aarch64_linux;;
  armv[67]*) tag=armv6_linux;;
  *) echo "Error: Your CPU $(uname -m) is not supported." >&2; exit 1 ;;
esac

USER=${USER:-$(id -un)}

if test -w /home/linuxbrew || /usr/bin/sudo -p "[sudo] Enter password for $USER to install Ruby: " mkdir -p /home/linuxbrew; then
	prefix=/home/linuxbrew/.linuxbrew/Homebrew/Library/Homebrew/vendor
	test -w /home/linuxbrew || sudo chown $USER: /home/linuxbrew
else
	prefix=~/.linuxbrew/Homebrew/Library/Homebrew/vendor
fi

echo >&2 "==> Installing Ruby to $prefix"
mkdir -p $prefix
curl -L https://homebrew.bintray.com/bottles-portable-ruby/portable-ruby-$version.$tag.bottle.tar.gz \
	| tar -xz -C $prefix
ln -sf $version $prefix/portable-ruby/current
echo >&2 '==> Installing successful'

export "PATH=$prefix/portable-ruby/current/bin:$PATH"
echo >&2 "==> $(command -v ruby)"
ruby --version 1>&2
echo >&2 "==> Add Ruby to your PATH by running:"
echo "PATH=$prefix/portable-ruby/current/bin"':$PATH'
